// index.js
Page({
    data: {
      conditions: [
        {id: 'all', name: '全部'},
        {id: 'electronics', name: '大连市'},
        {id: 'clothing', name: '沈阳市'},
        {id: 'home', name: '阜新市'},
        {id:'xm',name:'新民市'},
        {id:'tl',name:'铁岭市'}
      ],
      selectedIndex: 0,
      products: [
        {id: 1, name: '大连高新软件园店', category: 'electronics', address:'软件园15号楼A05', latitude:38.893706 ,longitude:121.5535},
        {id: 2, name: '大连黄河路锦辉店', category: 'electronics', address:'大连市沙河口区黄河路667号', latitude:38.916874 ,longitude:121.594165},
        {id: 3, name: '大连交大夜市店', category: 'electronics', address:'黄河路889号', latitude:38.919885 ,longitude:121.577113},
        {id: 4, name: '大连高新万达店', category: 'electronics', address:'大连市高新区黄浦路500号', latitude:38.868126,longitude:121.539952},
        {id: 5, name: '大连凯德和平广场店', category: 'electronics', address:'高尔基路695号凯德广场1层1号店', latitude:38.90374,longitude:121.593782},
        {id: 6, name: '大连中央大道吾悦店', category: 'electronics', address:'西安路107号吾悦广场一层', latitude:38.922048,longitude:121.591398},
        {id: 7, name: '大连罗斯福店', category: 'electronics', address:'大连市沙河口区西安路139号一层', latitude:38.918724 ,longitude:121.593},
        {id: 8, name: '大连甘井子万达店', category: 'electronics', address:'虹韵路6号甘井子万达广场1层', latitude:38.956153,longitude:121.546288},
        {id: 9, name: '大连青泥洼新玛特店', category: 'electronics', address:'大连市中山区友好街30号', latitude:38.922493,longitude:121.640271},
        {id: 10, name: '沈阳和平西塔街店', category: 'clothing', address:'西塔街25号', latitude:41.813642, longitude: 123.417568},
        {id: 11, name: '沈阳沈北吾悦店', category: 'clothing', address: '蒲河路33-1号吾悦广场1F', latitude:41.950801, longitude:123.420759},
        {id: 12, name: '沈阳航空航天地铁口店', category: 'clothing', address: '道义南大街25-1号', latitude:41.923418, longitude:123.410306},
        {id: 13, name: '沈阳皇姑万象汇店', category: 'clothing', address:'崇山东路万象汇B2层', latitude:41.840738, longitude:123.465107},
        {id: 14, name: '沈阳天地店', category: 'clothing', address: '北陵大街17号L1-12', latitude:41.831097, longitude: 123.43774},
        {id: 15, name: '沈阳浑南上亿广场店', category: 'clothing', address: '沈营大街587号上亿广场', latitude: 41.696653, longitude:123.430006},
        {id: 16, name: '沈阳长白万象汇店', category: 'clothing', address: '长白北路199号华润万象汇', latitude:41.753294, longitude:123.40628},
        {id:17, name: '沈阳大悦城店', category: 'clothing', address: '小东路6号大悦城A馆一层', latitude:41.808813, longitude:123.47341},
        {id: 18, name: '沈阳北一路万达店', category: 'clothing', address: '北一中路1号万达广场', latitude:41.819733, longitude:123.386385},
        {id: 19, name: '阜新细河大润发店', category: 'home', address: '中华路63号大润发1F', latitude:42.022547, longitude:121.663031},
        {id: 20, name: '阜新万达店', category: 'home', address: '解放大街甲17号1055A', latitude:42.040666, longitude:121.652569},
        {id: 21, name:'新民友谊商城店',category:'xm',address:'辽河大街135号友谊商城A1024',latitude:42.003988,longitude:122.834185},
        {id: 22, name:'铁岭银州新玛特店',category:'tl',address:'南马路29号大商新玛特1-29号店铺',latitude:42.30475,longitude:123.856501},
        {id: 23, name:'铁岭开源植物园店',category:'tl',address:'新城街道熊谷阳光花园C栋植物园',latitude:42.552013,longitude:124.060456}

        // ... 更多商品
      ],
      currentLatitude: 38.893706,  // 初始纬度
      currentLongitude:121.5535,// 初始经度
      markers: [] // 地图标记
    },
    onConditionChange: function(e) {/* 在用户选择不同筛选条件时被调用 */
      const selectedIndex = e.detail.value;
      this.setData({selectedIndex});
      this.updateProductList();
      this.updateMarkers();
    },
    updateProductList: function() {
      const selectedCondition = this.data.conditions[this.data.selectedIndex].id;
      let filteredProducts = this.data.products;
      if (selectedCondition !== 'all') {
        filteredProducts = filteredProducts.filter(product => product.category === selectedCondition);
      }
      this.setData({filteredProducts});
    },





    // 地图
    updateMarkers: function() {
      const selectedCondition = this.data.conditions[this.data.selectedIndex].id;
      let markers = [];
      if (selectedCondition === 'all') {
        markers = this.data.products.map(product => {
          return {
            id: product.id,
            latitude: product.latitude,
            longitude: product.longitude,
            title: product.name,
            iconPath: 'cloud://cloud1-1gprl58p0dd92671.636c-cloud1-1gprl58p0dd92671-1326489125/image/location.png', // 标记图标路径
            width: 50,
            height: 50
          };
        });
      } else {
        markers = this.data.products.filter(product => product.category === selectedCondition).map(product => {
          return {
            id: product.id,
            latitude: product.latitude,
            longitude: product.longitude,
            title: product.name,
            iconPath: 'cloud://cloud1-1gprl58p0dd92671.636c-cloud1-1gprl58p0dd92671-1326489125/image/location.png', // 标记图标路径
            width: 50,
            height: 50
          };
        });
      }
      this.setData({markers});
    },
    onProductTap: function(e) {
        const productId = e.currentTarget.dataset.id;
        const product = this.data.products.find(p => p.id === productId);
        if (product) {
          wx.navigateTo({
            url: `/pages/mapPage/mapPage?latitude=${product.latitude}&longitude=${product.longitude}&title=${product.name}`
          });
        }
      }
  });